/**
 * @PROJECT 
 * @DATE 2018年7月12日 上午10:28:02
 * @AUTHOR LUYU
 */
package com.kool.system.action.roleGroup;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import com.kool.system.bean.SyRoleBean;
import com.kool.system.bean.SyRoleGroupBean;
import com.kool.system.bean.SyUserInfoBean;
import com.kool.system.bean.io.SYUSRQRYZBean;
import com.kool.system.bean.io.role.SYROLEINFYBean;
import com.kool.system.bean.io.roleGroup.SYGROUPCODEXBean;
import com.kool.system.bean.io.roleGroup.SYROLEGROUPINFYBean;
import com.kool.system.service.RoleGroupService;

/**
 * @DESCRIBE 查询角色组详情
 * @AUTHOR LUYU
 * @DATE 2018年7月12日 上午10:28:02
 *
 */
@Controller
public class QueryDetailGourpAction implements IBaseAction {
	@Autowired
	private RoleGroupService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/sys/roleGroup/queryRoleGroupDetail", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("SYGROUPCODEX");
		JSONObject propIn = secIn.getJSONObject(0);
		SYGROUPCODEXBean ioIn = JSON.toJavaObject(propIn, SYGROUPCODEXBean.class);

		SyRoleGroupBean beanIn = (SyRoleGroupBean) BeanUtils.io2Bean(ioIn, SyRoleGroupBean.class, "srg");

		SyRoleGroupBean groupDtl = service.queryOne(beanIn);
		List<SyRoleBean> listRoles = service.queryRolesOfGroup(beanIn.getSrgGroupCode());
		List<SyUserInfoBean> listUsers = service.queryUsersOfGroup(beanIn.getSrgGroupCode());

		JSONObject pkgOut = new JSONObject();
		JSONArray secOutGroup = new JSONArray();
		SYROLEGROUPINFYBean ioGroup = (SYROLEGROUPINFYBean) BeanUtils.bean2Io(groupDtl, SYROLEGROUPINFYBean.class);
		secOutGroup.add(JSON.toJSON(ioGroup));
		pkgOut.put("SYROLEGROUPINFY", secOutGroup);

		JSONArray secOutRoles = new JSONArray();
		for (SyRoleBean role : listRoles) {
			SYROLEINFYBean ioRole = (SYROLEINFYBean) BeanUtils.bean2Io(role, SYROLEINFYBean.class);
			JSONObject joRole = (JSONObject) JSON.toJSON(ioRole);
			GlobalDefineUtils.addTransformation(joRole, "state", "SRO_STATE");
			secOutRoles.add(joRole);
		}
		pkgOut.put("SYROLEINFY", secOutRoles);

		JSONArray secOutUsers = new JSONArray();
		for (SyUserInfoBean user : listUsers) {
			SYUSRQRYZBean ioUser = (SYUSRQRYZBean) BeanUtils.bean2Io(user, SYUSRQRYZBean.class);
			JSONObject joUser = (JSONObject) JSON.toJSON(ioUser);
			GlobalDefineUtils.addTransformation(joUser, "type", "SUI_TYPE");
			GlobalDefineUtils.addTransformation(joUser, "state", "SUI_STATE");
			GlobalDefineUtils.addTransformation(joUser, "hasKey", "SUI_HAS_KEY");
			secOutUsers.add(joUser);
		}
		pkgOut.put("SYUSRQRYZ", secOutUsers);

		return pkgOut;
	}

}
